package com.yili.oa.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yili.oa.entity.SearchBase;
import com.yili.oa.entity.TableBase;
import com.yili.oa.models.DealersLevel;
import com.yili.oa.service.DealersLevelService;
import com.yili.oa.utils.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DealersLevelController {

    @Autowired
    DealersLevelService service;

    @RequestMapping("dealersLevel/get")
    @ResponseBody
    public TableBase<DealersLevel> get(SearchBase searchBase){
        PageHelper.startPage(searchBase.getPage(), searchBase.getLimit());
        List<DealersLevel> list = service.get(searchBase);
        PageInfo pageInfo = new PageInfo<>(list, searchBase.getLimit());
        TableBase<DealersLevel> table = new TableBase<>();
        table.setCode(0);
        table.setData(list);
        table.setMsg("");
        table.setCount(pageInfo.getTotal());
        return table;
    }

    @RequestMapping("dealersLevel/getById")
    @ResponseBody
    public DealersLevel getById(String id){
        SearchBase searchBase = new SearchBase();
        searchBase.setId(id);
        List<DealersLevel> list = service.get(searchBase);
        if(ListUtils.isNullOrEmpty(list)){
            return null;
        }else{
            return list.get(0);
        }

    }

    @RequestMapping("dealersLevel/add")
    @ResponseBody
    public Map<String,String> add(@RequestBody DealersLevel dealersLevel) {
        Map<String, String> map = new HashMap<>();
        service.add(dealersLevel);
        map.put("result", "success");
        return map;
    }

    @RequestMapping("dealersLevel/update")
    @ResponseBody
    public Map<String,String> update(@RequestBody DealersLevel dealersLevel) {
        Map<String, String> map = new HashMap<>();
        service.update(dealersLevel);
        map.put("result", "success");
        return map;
    }

    @RequestMapping("dealersLevel/delete")
    @ResponseBody
    public Map<String,String> delete(@RequestBody List<Long> ids) {
        Map<String, String> map = new HashMap<>();
        service.delete(ids);
        map.put("result", "success");
        return map;
    }
}
